/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Jan-16-2024 10:34:29
*
*/


#include "tim.h"
#include "gd32l23x.h"
#include "led.h"
#include "device.h"
#include "stdio.h"
#include "input.h"

#ifdef PWM_LED_OE


#define PERID_PWM 1990
void update_TIME211(void)
{
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 300 * 1); // 设置占空比50%
    //TIMER_CH0CV(TIMER2) = (uint32_t)5;
    timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    //uint32_t ctl = TIMER_CHCTL0(TIMER2);
    //ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMCTL);
    //ctl |= (uint32_t)TIMER_OC_MODE_PWM0;
    //TIMER_CHCTL0(TIMER2) = ctl;
    timer_channel_output_shadow_config(TIMER2, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);
    //ctl = TIMER_CHCTL0(TIMER2);
    //ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMSEN);
    //ctl |= (uint32_t)TIMER_OC_SHADOW_ENABLE;
    //TIMER_CHCTL0(TIMER2) = ctl;
    //timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_SINGLE);
    timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_REPETITIVE);
    //TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_SPM;
    timer_enable(TIMER2);
    //TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_CEN;
}

static volatile uint16_t d_v = 290, cv = 0,  pre_d_v = 0xF0F0;
static uint8_t update = 0;
#define DELTA_V 0x10

void update_led_bl(void)
{
    uint16_t temp_v;
    static uint8_t a;

    if(update++ >= 7) {
        update = 0;
        //adc_buf[0]=1;
        //if(adc_buf[0]==0)adc_buf[0]=0x10;
        temp_v = adc_buf[0] & 0xFFF; //1800;//0x10;//
        //temp_v = 0X2;

        //log_debug("%d\t\t%d\t\t%d\t\t%08x\t---\n", d_v, cv, temp_v, adc_buf[0]);
        if(temp_v > cv) {
            if(temp_v > cv + DELTA_V) {
                cv += DELTA_V;

            } else {
                cv = temp_v;
                //log_debug("1-%d\n", cv);
            }

        } else if(temp_v < cv) {
            if(temp_v + DELTA_V < cv) {
                cv -= DELTA_V;
                //log_debug("2-%d\n", cv);

            } else {
                cv = temp_v;//
                //log_debug("3- %d\n", cv);
            }
        }

        //d_v = (cv >> 2);//4095/16*6=
        d_v = ((cv >> 4) & 0xFFF) * 6 ; //4095/16*6=
        d_v = (d_v & 0xFFF);

        if(d_v > (PERID_PWM - 80)) {
            d_v = PERID_PWM - 80;
        }

        if(a++ >= 20) {
            a = 0;
            //log_debug("led bl:%d\t\t%d => %d\t\t%08x\t\n", d_v, cv, temp_v, adc_buf[0]);
        }

        //d_v = (cv>>2)*828/1024;
        //log_debug("%d\t\t%d\t\t%d\t\t%08x\t\n", d_v, cv, temp_v, adc_buf[0]);
    }

    //timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 3 * 1); // 设置占空比50%
    //d_v = 1600;
    //d_v = 1024 - d_v;
    /*if(d_v>828){
        d_v=828;
    }else if(d_v<10){
        d_v=10;

    }*/
    //if(d_v == pre_d_v)return;
    //pre_d_v = d_v;
    //log_debug("\t%d\t%x\t%x\n", d_v, cv, adc_buf[0]);
#ifdef DEBUG_PWM_LED_VALUE
    d_v = 200;
#else
    TIMER_CH0CV(TIMER2) = d_v;
    //timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    uint32_t ctl = TIMER_CHCTL0(TIMER2);
    ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMCTL);
    ctl |= (uint32_t)TIMER_OC_MODE_PWM0;
    TIMER_CHCTL0(TIMER2) = ctl;
    //timer_channel_output_shadow_config(TIMER2, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);
    ctl = TIMER_CHCTL0(TIMER2);
    ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMSEN);
    ctl |= (uint32_t)TIMER_OC_SHADOW_ENABLE;
    TIMER_CHCTL0(TIMER2) = ctl;
    //timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_SINGLE);
    TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_SPM;
    //timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_REPETITIVE);
    //TIMER_CTL0(TIMER2) &= ~((uint32_t)TIMER_CTL0_SPM);
    //timer_enable(TIMER2);
    TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_CEN;
#endif
}

//PWM OUT


timer_oc_parameter_struct timer2_ocintpara;
timer_parameter_struct timer2_initpara;

volatile uint8_t lcd_backlight = 0;

void TIME2_Init(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_4);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
    rcu_periph_clock_enable(RCU_TIMER2);
    //timer_disable(TIMER2);
    //timer_auto_reload_shadow_disable(TIMER2);
    //timer_deinit(TIMER2);
    //rcu_periph_clock_enable(RCU_TIMER2);
    /* TIMER1 configuration */
    timer2_initpara.prescaler        = 63;  //预分频 108M / 107 + 1 = 1M
    timer2_initpara.alignedmode      = TIMER_COUNTER_EDGE;  //无中央对齐计数模式(边沿对齐模式)，DIR位指定了计数方向
    timer2_initpara.counterdirection  = TIMER_COUNTER_UP;   //向上计数
    timer2_initpara.period           = PERID_PWM; // 1M / 1000 = 1000Hz
    timer2_initpara.clockdivision    = TIMER_CKDIV_DIV2;
    //timer_channel_output_struct_para_init(&timer2_ocintpara);
    timer2_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer2_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_init(TIMER2, &timer2_initpara);
    //timer_auto_reload_shadow_enable(TIMER2);
    //timer_autoreload_value_config(TIMER2, 1900);
    timer_channel_output_config(TIMER2, TIMER_CH_0, &timer2_ocintpara); //选择通道1
    update_led_bl();
}

#ifdef LCD_PWM_BL
void update_lcd_bl(void)
{
    uint32_t temp_v;
    static uint8_t a;

    /*if(update++ >= 7) {
        update = 0;
        temp_v = adc_buf[0];//1800;//0x10;//
        //log_debug("%d\t\t%d\t\t%d\t\t%08x\t---\n", d_v, cv, temp_v, adc_buf[0]);
        if(temp_v > cv) {
            if(temp_v > cv + DELTA_V) {
                cv += DELTA_V;
            } else {
                cv = temp_v;
                //log_debug("1-%d\n", cv);
            }
        }else if(temp_v < cv) {
            if(temp_v < cv + DELTA_V) {
                cv -= DELTA_V;
                //log_debug("2-%d\n", cv);

            } else {
                cv = temp_v;//
             //log_debug("3- %d\n", cv);
            }
        }

        //d_v = (cv >> 2);//4095/16*6=
        d_v = (cv >> 4)*6+10;//4095/16*6=
        //d_v = (cv>>2)*828/1024;
        //log_debug("%d\t\t%d\t\t%d\t\t%08x\t\n", d_v, cv, temp_v, adc_buf[0]);
    }
    */
    //timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, 3 * 1); // 设置占空比50%
    //d_v = 1600;
    //d_v = 1024 - d_v;
    /*if(d_v>828){
        d_v=828;
    }else if(d_v<10){
        d_v=10;

    }*/
    //if(d_v == pre_d_v)return;
    //pre_d_v = d_v;
    if(lcd_backlight == 5) {
        lcd_backlight = 0;
        log_debug("bl_pwm_ctrl_on\n");
        bl_pwm_ctrl_on();

    } else if(lcd_backlight == 3) {
        log_debug("lcd_backlight=3\n");
        lcd_backlight = 1;
        BL_CTRL_ON;

    } else if(lcd_backlight == 1) {
    } else {
        if(a++ > 200) {
            a = 0;
            log_debug("\t%d\t%d\t%x\t%x\n", 1900 - d_v / 2, d_v, cv, adc_buf[0]);
        }

        TIMER_CH1CV(TIMER2) = 1900 - d_v / 2;
        uint32_t ctl = TIMER_CHCTL0(TIMER2);
        timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
        /*ctl = TIMER_CHCTL0(TIMER2);
        ctl &= (~(uint32_t)TIMER_CHCTL0_CH1COMCTL);
        ctl |= (uint32_t)((uint32_t)(TIMER_OC_MODE_PWM0) << 8U);
        TIMER_CHCTL0(TIMER2) = ctl;*/
        //uint32_t ctl = TIMER_CHCTL0(TIMER2);
        //ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMCTL);
        //ctl |= (uint32_t)TIMER_OC_MODE_PWM0;
        // TIMER_CHCTL0(TIMER2) = ctl;
        //timer_channel_output_shadow_config(TIMER2, TIMER_CH_0, TIMER_OC_SHADOW_ENABLE);
        //ctl = TIMER_CHCTL0(TIMER2);
        //ctl &= (~(uint32_t)TIMER_CHCTL0_CH0COMSEN);
        //ctl |= (uint32_t)TIMER_OC_SHADOW_ENABLE;
        //TIMER_CHCTL0(TIMER2) = ctl;
        timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_ENABLE);
        /*ctl = TIMER_CHCTL0(TIMER2);
            ctl &= (~(uint32_t)TIMER_CHCTL0_CH1COMSEN);
            ctl |= (uint32_t)((uint32_t)(TIMER_OC_SHADOW_ENABLE) << 8U);
            TIMER_CHCTL0(TIMER2) = ctl;*/
        //timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_SINGLE);
        //TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_SPM;
        //timer_single_pulse_mode_config(TIMER2, TIMER_SP_MODE_REPETITIVE);
        //TIMER_CTL0(TIMER2) &= ~((uint32_t)TIMER_CTL0_SPM);
        //timer_enable(TIMER2);
        //TIMER_CTL0(TIMER2) |= (uint32_t)TIMER_CTL0_CEN;
    }
}

void bl_pwm_ctrl_on()
{
    rcu_periph_clock_enable(RCU_GPIOC);
    gpio_af_set(GPIOC, GPIO_AF_1, GPIO_PIN_7);
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
    rcu_periph_clock_enable(RCU_TIMER2);
    //timer_disable(TIMER2);
    //timer_auto_reload_shadow_disable(TIMER2);
    //timer_deinit(TIMER2);
    //rcu_periph_clock_enable(RCU_TIMER2);
    /* TIMER1 configuration */
    timer2_initpara.prescaler        = 63;  //预分频 108M / 107 + 1 = 1M
    timer2_initpara.alignedmode      = TIMER_COUNTER_EDGE;  //无中央对齐计数模式(边沿对齐模式)，DIR位指定了计数方向
    timer2_initpara.counterdirection  = TIMER_COUNTER_UP;   //向上计数
    timer2_initpara.period           = 1900; // 1M / 1000 = 1000Hz
    timer2_initpara.clockdivision    = TIMER_CKDIV_DIV2;
    //timer_channel_output_struct_para_init(&timer2_ocintpara);
    timer2_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer2_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_init(TIMER2, &timer2_initpara);
#ifdef LCD_PWM_BL
    timer_channel_output_config(TIMER2, TIMER_CH_1, &timer2_ocintpara);
    update_lcd_bl();
#endif
}

#endif


#endif



